package com.feng.cloud.common.validator;

import com.feng.cloud.common.annotation.IsMobile;
import com.feng.cloud.common.constants.validator.RegexpConstant;
import com.feng.cloud.common.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @ClassName MobileValidator
 * @Description 校验是否为合法的手机号码
 * @Author Better Feng
 * @Date 2019/12/18 14:23
 * @Version 1.0
 **/
public class MobileValidator implements ConstraintValidator<IsMobile, String> {
    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        try {
            if (StringUtils.isBlank(value)) {
                return true;
            } else {
                String regex = RegexpConstant.MOBILE_REGEXP;
                return ValidatorUtils.match(regex, value);
            }
        } catch (Exception e) {
            return false;
        }
    }
}
